package org.jeecg.modules.utils.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.transtella.messageCenter.IMessageManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.basic.customer.entity.BasicCustomer;
import org.jeecg.modules.business.doc.business.entity.WmsInReceipt;
import org.jeecg.modules.business.doc.business.mapper.WmsInReceiptMapper;
import org.jeecg.modules.business.doc.outbound.entity.WmsOutReceipt;
import org.jeecg.modules.business.doc.outbound.mapper.WmsOutReceiptMapper;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTask;
import org.jeecg.modules.business.outbound.mapper.WmsOutReceiptTaskMapper;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTask;
import org.jeecg.modules.business.warehouse.mapper.WmsInReceiptTaskMapper;
import org.jeecg.modules.utils.entity.WmsBaseDocument;
import org.jeecg.modules.utils.service.IWmsBaseDocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "单据状态流转")
@RestController
@RequestMapping("/documentStatusMove")
@Slf4j
public class DocumentStatusMoveController {

    @Autowired
    private IWmsBaseDocumentService wmsBaseDocumentService;

    @Autowired
    private WmsInReceiptTaskMapper wmsInReceiptTaskMapper;

    @Autowired
    private WmsInReceiptMapper wmsInReceiptMapper;

    @Autowired
    private WmsOutReceiptMapper wmsOutReceiptMapper;

    @Autowired
    private WmsOutReceiptTaskMapper wmsOutReceiptTaskMapper;

    @Autowired
    private IMessageManager messageManager;


    @ApiOperation(value = "提交单据", notes = "提交单据")
    @PostMapping(value = "/commit")
    public Result commitDocument(@RequestParam String id) {
        WmsBaseDocument document = wmsBaseDocumentService.getById(id);
        if (document == null) {
            return Result.error("未能确定单据类型!");
        }
        switch (document.getTableName()) {
            case "wms_in_receipt": {
                WmsInReceipt inReceipt = wmsInReceiptMapper.selectById(id);
                if (ObjectUtil.isNotEmpty(inReceipt.getWmsInReceiptTaskId())) {
                    WmsInReceiptTask inReceiptTask = wmsInReceiptTaskMapper.selectById(inReceipt.getWmsInReceiptTaskId());
                    if ("-1".equals(inReceiptTask.getDocumentStatus())) {
                        return Result.error("提交失败，所属入库任务已关闭");
                    }
                }
                break;
            }
            case "wms_out_receipt": {
                WmsOutReceipt outReceipt = wmsOutReceiptMapper.selectById(id);
                if (ObjectUtil.isNotEmpty(outReceipt.getWmsOutReceiptTaskId())) {
                    WmsOutReceiptTask outReceiptTask = wmsOutReceiptTaskMapper.selectById(outReceipt.getWmsOutReceiptTaskId());
                    if ("-1".equals(outReceiptTask.getDocumentStatus())) {
                        return Result.error("提交失败，所属出库任务已关闭");
                    }
                }
                break;
            }
            default:
                break;
        }
        int committedRows = wmsBaseDocumentService.updateDocumentToCommitted(document.getId(), document.getTableName());
        if (committedRows != 1) {
            return Result.OK().error500("提交失败,只能提交已保存状态的数据");
        }
//        messageManager.sendCommand(req.getHeader(CommonConstant.X_ACCESS_TOKEN),
//                "commit." + document.getTableName(), document.getId());
        return Result.OK("提交成功");
    }
}
